﻿using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace Barbarian.Win
{
    internal class Player
    {
        #region Dependancies 
        private ContentManager _content;
        private AnimationPlayer _sprite;
        private PlayerAnimator _animator;
        #endregion

        #region Member variables
        private Animation _walkingAnimation;
        private Animation _idleAnimation;
        private Rectangle _localBounds;
        private SpriteEffects _flip = SpriteEffects.None;
        private Vector2 _position;
        #endregion

        #region Properties
        public Vector2 Position
        {
            get { return _position; }
            set { _position = value; }
        }
        #endregion

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="content"></param>
        public Player(ContentManager content, Vector2 position)
        {
            _content = content;
            _position = position;

            LoadContent();

            // set initia animation
            _sprite.PlayAnimation(_idleAnimation);
        }

        /// <summary>
        /// Load Player Content
        /// </summary>
        private void LoadContent()
        {
            _walkingAnimation = new Animation(_content.Load<Texture2D>("Barbarian/Walking"), 0.1f, true, 63, 48, new int[] {0} );
            _idleAnimation = new Animation(_content.Load<Texture2D>("Barbarian/Idle"), 0.1f, false);

            _animator = new PlayerAnimator(_content.Load<Texture2D>("Barbarian/frameset"), 2, 64);
            _animator.SetupPlayer(PlayerAnimator.AnimationName.Idle, PlayerAnimator.InputSource.Keyboard, 0);
            
            // Calculate bounds within texture size.            
            int width = (int)(_idleAnimation.FrameWidth * 0.4);
            int left = (_idleAnimation.FrameWidth - width) / 2;
            int height = (int)(_idleAnimation.FrameWidth * 0.8);
            int top = _idleAnimation.FrameHeight - height;
            _localBounds = new Rectangle(left, top,- width, height);
        }

        public void Update()
        {
            HandleInput();
        }

        /// <summary>
        /// Draw player
        /// </summary>
        /// <summary>
        /// Draws the animated player.
        /// </summary>
        public void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            // Draw sprite.
            _sprite.Draw(gameTime, spriteBatch, _position, _flip);

            //_animator.Draw(gameTime, spriteBatch, new Vector2(_position.X, 100), _flip);
        }

        /// <summary>
        /// Handle user input
        /// </summary>
        private void HandleInput()
        {
            KeyboardState keyboardState = Keyboard.GetState();

            _animator.HandleInput();

            if (keyboardState.IsKeyDown(Keys.Space))
              _animator.Skin = 1 - _animator.Skin;

            if(keyboardState.IsKeyDown(Keys.Right))
                _sprite.PlayAnimation(_walkingAnimation);
            else
                _sprite.PlayAnimation(_idleAnimation);            
        }
    }    
}
